package main

import (
	"fmt"
	"path"
	"path/filepath"
	"strings"

	"gameserver/gcommon"
	_ "gameserver/server"
	"gameserver/server/Gateway/service"

	"gitee.com/night-tc/gobige/common"
	"gitee.com/night-tc/gobige/entity"
	"gitee.com/night-tc/gobige/global"
	"gitee.com/night-tc/gobige/logger"
	"gitee.com/night-tc/gobige/threads"
	"gitee.com/zhenghexcs/flags"
	"github.com/spf13/viper"
)

func main() {
	flags.SetFlag(common.ServerName, strings.ToUpper(common.ServerName), "服务器名字，表示server.json中的名字", "Gateway")
	flags.Parse()
	vmap := viper.New()
	vmap.SetConfigFile(flags.GetFlagByString(common.ServerJson))
	// fmt.Println(flags.GetFlagByString(common.ServerJson))
	if err := vmap.ReadInConfig(); err != nil {
		panic(fmt.Errorf("加载配置文件失败 %v", err))
	}
	logger.Init(path.Join(common.GetResPath(), "config", "log.toml"), flags.GetFlagByString(common.ServerName), threads.NewThreadGo())
	sinfo := gcommon.LoadServerConfig(vmap, flags.GetFlagByString(common.ServerName), global.ServerTypeGateway)
	global.SetCfgInst(sinfo)
	if err := entity.GetTypeMgr().LoadDefs(filepath.Join(common.GetResPath(), "entity")); err != nil {
		panic(err)
	}
	srv := service.NewService(sinfo)
	defer logger.Flush()

	srv.Run()
}
